#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include <float.h>
#if defined(TEST_TARGET_flmath)
#include <varch/command.h>
#include <varch/unitt.h>
#include <varch/flmath.h>
#else  
#include "init.h"
#include "command.h"
#include "unitt.h"
#include "kern.h"
#include "flmath.h"
#endif

static char buffer[1024] = {0};

/************************************************************************************/
/************************************* Unit Test ************************************/
/************************************************************************************/

// #define EXIT_TEST
extern uint64_t unitt_clock(void);

static int u_test_0(void)
{
    for (int i = 0; i < 1000; i++)
    {
        
    }
    
    return UNITT_E_OK;
}

static void unitt_task(void)
{
    static UNITT_TCASE rand_tests[] = {
        UNITT_TCASE(u_test_0),
    };

    static UNITT suites[] = {
        { "flmath suite", rand_tests, sizeof(rand_tests) / sizeof(rand_tests[0]) , unitt_clock },
    };

    UNITT_EXE(suites);
}

/************************************************************************************/
/************************************* Base Test ************************************/
/************************************************************************************/

static void test_base(void)
{
    floatl p = floatl_mul(FLOATL_PI, floatl(1.0 / 2.0));

    floatl sin = floatl_sin(p);
    printf("sin %s\r\n", floatl_show(sin, buffer, sizeof(buffer), "%f"));

    floatl cos = floatl_cos(p);
    printf("cos %s\r\n", floatl_show(cos, buffer, sizeof(buffer), "%f"));

    floatl tan = floatl_tan(p);
    printf("tan %s\r\n", floatl_show(tan, buffer, sizeof(buffer), "%f"));

    floatl exp = floatl_exp(floatl(0.0));
    printf("exp %s\r\n", floatl_show(exp, buffer, sizeof(buffer), "%f"));

    floatl log = floatl_ln(floatl(1.0));
    printf("log %s\r\n", floatl_show(log, buffer, sizeof(buffer), "%f"));

    floatl sqrt = floatl_sqrt(floatl(3.0));
    printf("sqrt %s\r\n", floatl_show(sqrt, buffer, sizeof(buffer), "%f"));

    floatl pow = floatl_pow(floatl(2.0), floatl(3.0));
    printf("pow %s\r\n", floatl_show(pow, buffer, sizeof(buffer), "%f"));
}

/************************************************************************************/
/*************************************  Command  ************************************/
/************************************************************************************/

static void usage(void)
{
    printf(
"Usage: floatl [opt] [arg] ...\n"
"\n"
"options:\n"
"    -e <execute>        Specifies the function to execute, the default is the <base> test\n"
"                        <base>      Test base function\n"
"                        <ut>        Unit test\n"
"                        <define>    Test define function\n"
"                        <cal>       Calculate string math expression\n"
"                        <gen>       Generate the floatl configuration file code segment, need specify -f -b\n"
"                        <print>     Print an floatl number\n"
"                        <error>     Function that tests for floatl errors\n"
"    -l <format>         Format string, 10d, 08x, ...\n"
"    -o <op function>    Operate function, add, sub, mul, div, ...\n"
"    -n <floatl>           floatl number expression\n"
"    -f <filename>       File name, temporarily store configuration code segment\n"
"    -b <bits>           Maximum number of configured bits\n"
"    -h                  Print help\n"
"    -v                  Print version\n"
"    -u [<period>]       Unit test period, unit ms, the default is 1000ms\n"
"\n"

    );
}

static int test(int argc, char *argv[])
{
    char *execute = NULL;
    int ut_period = 1000;

    /* reset getopt */
    command_opt_init();

    while (1)
    {
        int opt = command_getopt(argc, argv, "e:hvu::");
        if (opt == -1) break;

        switch (opt) 
        {
        case 'u' :
            if (command_optarg) ut_period = atoi(command_optarg);
            break;
        case 'e' :
            execute = command_optarg;
            break;
        case 'v' :
            printf("floatl version %d.%d.%d\r\n", FLMATH_V_MAJOR, FLMATH_V_MINOR, FLMATH_V_PATCH);
            return 0;
        case '?':
            printf("Unknown option `%c`\r\n", command_optopt);
            return -1;
        case 'h' : 
        default:
            usage();
            return 0;
        }
    }

    if (execute)
    {
        if (!strcmp(execute, "base"))
        {
            test_base();
        }
        else if (!strcmp(execute, "ut"))
        {
            srand((uint32_t)time(NULL));
            #if defined(TEST_TARGET_flmath)
            while (1)
            {
                unitt_task();
                usleep(1000 * ut_period);
            }
            #else  
            printf("create task %d\r\n", task_create(ut_period, unitt_task));
            #endif
        }
    }
    else  
    {
        test_base();
    }
    
    return 0;
}

/************************************************************************************/
/************************************ Test entry ************************************/
/************************************************************************************/

#if defined(TEST_TARGET_flmath)
int main(int argc, char *argv[])
{
    return test(argc, argv);
}
#else 
void test_flmath(void)
{
    command_export("flmath", test);
}
init_export_app(test_flmath);
#endif 

